package com.activepad.module.customer.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.activepad.common.interfaces.SurveyDaoImpl;
import com.activepad.module.customer.dao.EnterpriseCustomerDao;
import com.activepad.module.customer.pojo.AccountVo;
import com.activepad.module.customer.pojo.CustomerVo;
import com.activepad.module.entity.customer.EnterpriseAccount;
import com.activepad.module.entity.customer.EnterpriseCustomer;
import com.common.bean.SearchBean;
import com.common.util.BeanUtils;

/**
 * 
 * 企业客户数据访问接口实现类
 * 
 * @author 孙树林
 * 
 */
public class EnterpriseCustomerDaoImpl extends SurveyDaoImpl implements EnterpriseCustomerDao {

	/**
	 * 查询企业客户数据访问方法
	 * 
	 * @author 孙树林
	 * @param enterpriseCustomer
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<CustomerVo> searchEnterpriseCustomer(SearchBean searchBean) {
		List<EnterpriseCustomer> enterpriseCustomers = hibernateDataAccess.criteriaOfSimple(searchBean, EnterpriseCustomer.class);
		List<CustomerVo> customerVos = new ArrayList<CustomerVo>();
		for (EnterpriseCustomer enterpriseCustomer : enterpriseCustomers) {
			CustomerVo vo = new CustomerVo();
			BeanUtils.copyPropertiesNotNull(enterpriseCustomer, vo);
			customerVos.add(vo);
		}
		return customerVos;
	}

	/**
	 * 根据ID查找企业客户数据访问方法
	 * 
	 * @author 孙树林
	 * @param id
	 * @return
	 */
	@Override
	public CustomerVo findEnterpriseCustomer(Integer id) {
		EnterpriseCustomer enterpriseCustomer = (EnterpriseCustomer) hibernateDataAccess.getEntity(EnterpriseCustomer.class, id);
		CustomerVo vo = new CustomerVo();
		BeanUtils.copyPropertiesNotNull(enterpriseCustomer, vo);
		// 启用过滤器
		hibernateDataAccess.getSession().enableFilter("effevtiveFilter").setParameter("isEffective", "130001");
		// 客户账户资料
		Iterator<EnterpriseAccount> iterator = enterpriseCustomer.getEnterpriseAccounts().iterator();
		while (iterator.hasNext()) {
			EnterpriseAccount enterpriseAccount = iterator.next();
			AccountVo accountVo = new AccountVo();
			BeanUtils.copyPropertiesNotNull(enterpriseAccount, accountVo);
			vo.getAccountVos().add(accountVo);
		}
		return vo;
	}

	/**
	 * 保存企业客户资料信息
	 * 
	 * @author 孙树林
	 * @param customerVo
	 */
	@Override
	public void saveEnterpriseCustomer(CustomerVo customerVo) {
		EnterpriseCustomer enterpriseCustomer = new EnterpriseCustomer();
		if (customerVo != null) {
			BeanUtils.copyPropertiesNotNull(customerVo, enterpriseCustomer);
			hibernateDataAccess.saveEntity(enterpriseCustomer);
		}
	}

	/**
	 * 更新企业客户资料信息
	 * 
	 * @author 孙树林
	 * @param customerVo
	 */
	@Override
	public void updateEnterpriseCustomer(CustomerVo customerVo) {
		Integer id = customerVo.getId();
		EnterpriseCustomer enterpriseCustomer = (EnterpriseCustomer) hibernateDataAccess.loadEntity(EnterpriseCustomer.class, id);
		BeanUtils.copyPropertiesNotNull(customerVo, enterpriseCustomer);
		hibernateDataAccess.updateEntity(enterpriseCustomer);
	}

}